<?php
/**
 *
 *   User:King
 *   Date:2021/12/7 15:42
 */

namespace app\bank_api\controller;

use app\admin\library\Auth;
use app\bank_api\library\Jwtclass;
use think\exception\HttpResponseException;
use think\Loader;
use think\Request;

use think\Controller;
use think\Response;

class Base extends Controller
{
    protected $page = 0;
    protected $limit = 10;
    protected $auth;
    /**
     * 默认响应输出类型,支持json/xml
     * @var string
     */
    protected $responseType = 'json';

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    //员工信息
    protected $staffinfo;

    protected $isPostMsg = '请求格式错误';//非post请求错误信息
    protected $forbidden = 403;//拒绝请求

    public function _initialize()
    {
        parent::_initialize();
        $this->checkToken();
        $page = $this->request->param('page');
        $limit = $this->request->param('limit');

        $this->limit = (isset($limit) && $limit > 0) ? $limit : $this->limit;
        $this->page = (isset($page) && $page > 0) ? (($page-1)*$this->limit) : $this->page;
    }

    public function checkToken()
    {
//        $modulename = $this->request->module();
        $controllername = Loader::parseName($this->request->controller());
        $actionname = strtolower($this->request->action());

        $path = str_replace('.', '/', $controllername) . '/' . $actionname;
        $this->auth = Auth::instance();

        // 设置当前请求的URI
        $this->auth->setRequestUri($path);
        // 检测是否需要验证登录
        if (!$this->auth->match($this->noNeedLogin)) {
            $header = Request::instance()->header();
            if (!isset($header['token2']) || !$header['token2']) {
                $this->error('登陆后访问',null,401);
            } else {
                $checkJwtToken = Jwtclass::verifyJwt($header['token2']);
                if ($checkJwtToken['status'] == 1001) {
                    $this->staffinfo = $checkJwtToken['data'];
                    return true;
                }if ($checkJwtToken['status'] == 1004){
                    $this->error($checkJwtToken['msg'],null,401);
                }else{
                    $this->error($checkJwtToken['msg'],null,401);
                }
            }
        }
    }

    /**
     * 只接受post请求
     */
    public function onlyPost(){
        if(!$this->request->isPost()){
            $this->result($this->isPostMsg, null, $this->forbidden);
        }
    }

    /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型，支持json/xml/jsonp
     * @param array  $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
    {
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'time' => Request::instance()->server('REQUEST_TIME'),
            'data' => $data,
        ];
        // 如果未设置类型则自动判断
        $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);

        if (isset($header['statuscode'])) {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        } else {
            //未设置状态码,根据code值判断
            $code = $code >= 1000 || $code < 200 ? 200 : $code;
        }
        $response = Response::create($result, $type, $code)->header($header);
        throw new HttpResponseException($response);
    }


    /**
     * 操作成功返回的数据
     * @param string $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为1
     * @param string $type   输出类型
     * @param array  $header 发送的 Header 信息
     */
    protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
    {
        $this->result($msg, $data, $code, $type, $header);
    }

    /**
     * 操作失败返回的数据
     * @param string $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型
     * @param array  $header 发送的 Header 信息
     */
    protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
    {
        $this->result($msg, $data, $code, $type, $header);
    }
}
